package com.shiyu.exam.service.impl;

import com.shiyu.exam.properties.MinioProperties;
import com.shiyu.exam.service.FileUploadService;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.Date;
import java.util.UUID;

/**
 * projectName: com.atguigu.exam.service.impl
 *
 * @author: 赵伟风
 * description:
 */
@Service
@Slf4j
public class FileUploadServiceImpl implements FileUploadService {


    @Autowired
    private MinioProperties minioProperties;

    @Override
    public String uploadFile(MultipartFile file) {
        try {
            // 1.创建客户端对象
            MinioClient minioClient = MinioClient.builder()
                    //设置Minio服务器的地址
                    .endpoint(minioProperties.getEndpoint())
                    //设置用户名和密码
                    .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey())
                    .build();

            // 检查存储桶是否已经存在
            boolean isExist =
                    minioClient.bucketExists(BucketExistsArgs.builder().bucket(minioProperties.getBucketName()).build());
            if (isExist) {
                System.out.println("Bucket " + minioProperties.getBucketName() + " already exists.");
            } else {
                //如果桶不存在则创建它
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(minioProperties.getBucketName()).build());
            }

            // 获取上传文件的原始名称
            String originalFilename = file.getOriginalFilename();
            //获取文件的扩展名-- > 后缀名
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            //使用UUID重新生成文件名
            String priffix = UUID.randomUUID().toString().replaceAll("-", "");
            //对现在的日期进行格式化
            String format = DateFormatUtils.format(new Date(), "yyyy/MM/dd");
            //拼接文件名
            String newFileName = format + "/" + priffix + suffix;
            //获取输入流
            InputStream inputStream = file.getInputStream();
            //获取文件大小
            long size = file.getSize();

            //2.创建putObjectArgs对象
            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    .bucket(minioProperties.getBucketName())
                    .object(newFileName)
                    .stream(inputStream, size, -1)
                    .build();

            //使用putObject上传一个本地文件到Minio服务器的桶中
            minioClient.putObject(putObjectArgs);

            return minioProperties.getEndpoint() + "/" + minioProperties.getBucketName() + "/" + newFileName;

        } catch (Exception e) {
            System.out.println("Error occurred: " + e);
        }
        return null;
    }
}
